<?php
/*******************************************************************************
 * $Date:: Jan 18, 2011 4:39:12 PM											   $
 * $Revision: 													               $
 * $Author:: Tuan Duong													       $
 * $Comment::															       $
 ******************************************************************************/ 

/**
 * Class description.
 * @package: Lua
 * @subpackage: View
 * @author: Tuan Duong <bacduong[at]gmail[dot]com>
 * @version: 
 */
 
abstract class Lua_View_Abstract {
    /**
     * @var Zend_View_Abstract
     */
    protected $adapter;
    /**
     * @var Zend_Layout
     */
    private $layout;

    private $disabledLayout = false;
    private $noRender = false;

    public function __construct(Zend_View_Abstract $view)
    {
        $module = Lua_Registry::get(Lua_Constant_General::LUA_ROUTER)->getModule();
        $this->adapter = $view;
        $this->adapter->setBasePath(APPLICATION_PATH . DS . $module . DS . 'views');
        $this->adapter->addHelperPath(APPLICATION_PATH . DS . 'views' . DS . 'helpers');
        
        $this->layout = new Zend_Layout();
        $this->layout->setView($this->adapter);
        $this->layout->setLayoutPath(APPLICATION_PATH . DS . $module . DS . 'views' . DS . 'layouts');
    }

    /**
     * @return Zend_View_Abstract
     */
    public function getAdapter()
    {
        return $this->adapter;    
    }

    /**
     * Render Lua_View
     * @return string
     */
    public function render($scriptPath = '')
    {
        if (!empty($scriptPath)) {
            return $this->adapter->render($scriptPath);
        }
        if ($this->noRender) {
            return false;
        }
        $controller = Lua_Registry::get(Lua_Constant_General::LUA_ROUTER)->getController();
        $action = Lua_Registry::get(Lua_Constant_General::LUA_ROUTER)->getAction();
        $lowerAction = Lua_Util_Common::fromCamelCase($action, "-");
        if (!$this->disabledLayout) {
            $this->layout->content = $this->adapter->render($controller . DS . $lowerAction . '.phtml');
            return $this->layout->render();            
        } else {
            return $this->adapter->render($controller . DS . $lowerAction . '.phtml');
        }
    }

    public function disableLayout($disable = true)
    {
        $this->disabledLayout = $disable;
    }
    
    public function setNoRender($noRender = true) 
    {
        $this->noRender = $noRender;    
    }
    
    /**
     * Assign variable to the View adapter
     * @param string $key
     * @param mixed $value
     * @return void
     */
    public function assign($key, $value)
    {
        return $this->getAdapter()->{$key} = $value;
    }
}
